package com.example.tourstor;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class MainActivity extends FragmentActivity {

    private Fragment mFragments[];
    private RadioGroup radioGroup;
    private FragmentManager fragmentManager;
    private FragmentTransaction fragmentTransaction;
    private RadioButton rbtnHome;
    private RadioButton rbtnMerchant;
    private RadioButton rbtnPlan;
    private RadioButton rbtnGroup;
    private RadioButton rbtnMine;

    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        mFragments = new Fragment[5];
        fragmentManager = getSupportFragmentManager();
        mFragments[0] = fragmentManager.findFragmentById(R.id.fragment_home);
        mFragments[1] = fragmentManager.findFragmentById(R.id.fragment_merchant);
        mFragments[2] = fragmentManager.findFragmentById(R.id.fragment_plan);
        mFragments[3] = fragmentManager.findFragmentById(R.id.fragment_group);
        mFragments[4] = fragmentManager.findFragmentById(R.id.fragment_mine);
        fragmentTransaction = fragmentManager.beginTransaction().hide(mFragments[0])
                .hide(mFragments[1]).hide(mFragments[2]).hide(mFragments[3]).hide(mFragments[4]);
        fragmentTransaction.show(mFragments[0]).commit();

        rbtnHome = findViewById(R.id.radio_home);
        rbtnMerchant = findViewById(R.id.radio_merchant);
        rbtnPlan = findViewById(R.id.radio_plan);
        rbtnGroup = findViewById(R.id.radio_group);
        rbtnMine = findViewById(R.id.radio_mine);

        initView();
        //定义底部标签图片大小和位置
        Drawable drawable_home = getResources().getDrawable(R.drawable.home_pressed);
        //当这个图片被绘制时，给他绑定一个矩形 ltrb规定这个矩形
        drawable_home.setBounds(0, 0, 75, 75);
        //设置图片在文字的哪个方向
        rbtnHome.setCompoundDrawables(null, drawable_home, null, null);
        rbtnHome.setTextColor(Color.parseColor("#fb9d26"));


        radioGroup = findViewById(R.id.radioGroup);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                Log.i(TAG, "checkedId = " + checkedId);
                fragmentTransaction = fragmentManager.beginTransaction().hide(mFragments[0])
                        .hide(mFragments[1]).hide(mFragments[2]).hide(mFragments[3]).hide(mFragments[4]);
                initView();
                switch(checkedId){
                    case R.id.radio_home:
                        Log.i(TAG, "onCheckedChanged: radio_home");
                        //定义底部标签图片大小和位置
                        Drawable drawable_home = getResources().getDrawable(R.drawable.home_pressed);
                        //当这个图片被绘制时，给他绑定一个矩形 ltrb规定这个矩形
                        drawable_home.setBounds(0, 0, 75, 75);
                        //设置图片在文字的哪个方向
                        rbtnHome.setCompoundDrawables(null, drawable_home, null, null);
                        rbtnHome.setTextColor(Color.parseColor("#fb9d26"));
                        fragmentTransaction.show(mFragments[0]).commit();
                        break;
                    case R.id.radio_merchant:
                        Log.i(TAG, "onCheckedChanged: radio_merchant");
                        //定义底部标签图片大小和位置
                        Drawable drawable_merchant = getResources().getDrawable(R.drawable.shon_pressed);
                        //当这个图片被绘制时，给他绑定一个矩形 ltrb规定这个矩形
                        drawable_merchant.setBounds(0, 0, 75, 75);
                        //设置图片在文字的哪个方向
                        rbtnMerchant.setCompoundDrawables(null, drawable_merchant, null, null);
                        rbtnMerchant.setTextColor(Color.parseColor("#fb9d26"));
                        fragmentTransaction.show(mFragments[1]).commit();
                        break;
                    case R.id.radio_plan:
                        Log.i(TAG, "onCheckedChanged: radio_plan");
                        //定义底部标签图片大小和位置
                        Drawable drawable_plan = getResources().getDrawable(R.drawable.plan_pressed);
                        //当这个图片被绘制时，给他绑定一个矩形 ltrb规定这个矩形
                        drawable_plan.setBounds(0, 0, 75, 75);
                        //设置图片在文字的哪个方向
                        rbtnPlan.setCompoundDrawables(null, drawable_plan, null, null);
                        rbtnPlan.setTextColor(Color.parseColor("#fb9d26"));
                        fragmentTransaction.show(mFragments[2]).commit();
                        break;
                    case R.id.radio_group:
                        Log.i(TAG, "onCheckedChanged: radio_group");
                        //定义底部标签图片大小和位置
                        Drawable drawable_group = getResources().getDrawable(R.drawable.group_pressed);
                        //当这个图片被绘制时，给他绑定一个矩形 ltrb规定这个矩形
                        drawable_group.setBounds(0, 0, 75, 75);
                        //设置图片在文字的哪个方向
                        rbtnGroup.setCompoundDrawables(null, drawable_group, null, null);
                        rbtnGroup.setTextColor(Color.parseColor("#fb9d26"));
                        fragmentTransaction.show(mFragments[3]).commit();
                        break;
                    case R.id.radio_mine:
                        Log.i(TAG, "onCheckedChanged: radio_mine");
                        //定义底部标签图片大小和位置
                        Drawable drawable_mine = getResources().getDrawable(R.drawable.mine_pressed);
                        //当这个图片被绘制时，给他绑定一个矩形 ltrb规定这个矩形
                        drawable_mine.setBounds(0, 0, 75, 75);
                        //设置图片在文字的哪个方向
                        rbtnMine.setCompoundDrawables(null, drawable_mine, null, null);
                        rbtnMine.setTextColor(Color.parseColor("#fb9d26"));
                        fragmentTransaction.show(mFragments[4]).commit();
                        break;
                    default:
                        break;
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        Intent intent = getIntent();

        if(intent.getIntExtra("id", 0) == 2){
            fragmentTransaction = fragmentManager.beginTransaction().hide(mFragments[0])
                    .hide(mFragments[1]).hide(mFragments[2]).hide(mFragments[3]).hide(mFragments[4]);
            initView();
            //定义底部标签图片大小和位置
            Drawable drawable_plan = getResources().getDrawable(R.drawable.plan_pressed);
            //当这个图片被绘制时，给他绑定一个矩形 ltrb规定这个矩形
            drawable_plan.setBounds(0, 0, 75, 75);
            //设置图片在文字的哪个方向
            rbtnPlan.setCompoundDrawables(null, drawable_plan, null, null);
            rbtnPlan.setTextColor(Color.parseColor("#fb9d26"));
            fragmentTransaction.show(mFragments[2]).commit();

        }
        else if(intent.getIntExtra("id", 0) == 3){
            fragmentTransaction = fragmentManager.beginTransaction().hide(mFragments[0])
                    .hide(mFragments[1]).hide(mFragments[2]).hide(mFragments[3]).hide(mFragments[4]);
            initView();
            //定义底部标签图片大小和位置
            Drawable drawable_group = getResources().getDrawable(R.drawable.group_pressed);
            //当这个图片被绘制时，给他绑定一个矩形 ltrb规定这个矩形
            drawable_group.setBounds(0, 0, 75, 75);
            //设置图片在文字的哪个方向
            rbtnGroup.setCompoundDrawables(null, drawable_group, null, null);
            rbtnGroup.setTextColor(Color.parseColor("#fb9d26"));
            fragmentTransaction.show(mFragments[3]).commit();

        }
        else if(intent.getIntExtra("id", 0) == 4){
            fragmentTransaction = fragmentManager.beginTransaction().hide(mFragments[0])
                    .hide(mFragments[1]).hide(mFragments[2]).hide(mFragments[3]).hide(mFragments[4]);
            initView();
            //定义底部标签图片大小和位置
            Drawable drawable_mine = getResources().getDrawable(R.drawable.mine_pressed);
            //当这个图片被绘制时，给他绑定一个矩形 ltrb规定这个矩形
            drawable_mine.setBounds(0, 0, 75, 75);
            //设置图片在文字的哪个方向
            rbtnMine.setCompoundDrawables(null, drawable_mine, null, null);
            rbtnMine.setTextColor(Color.parseColor("#fb9d26"));
            fragmentTransaction.show(mFragments[4]).commit();

        }

    }

    private void initView() {
        //定义底部标签图片大小和位置
        Drawable drawable_home = getResources().getDrawable(R.drawable.home);
        //当这个图片被绘制时，给他绑定一个矩形 ltrb规定这个矩形
        drawable_home.setBounds(0, 0, 75, 75);
        //设置图片在文字的哪个方向
        rbtnHome.setCompoundDrawables(null, drawable_home, null, null);
        rbtnHome.setTextColor(Color.parseColor("#8a8a8a"));

        //定义底部标签图片大小和位置
        Drawable drawable_merchant = getResources().getDrawable(R.drawable.shop);
        //当这个图片被绘制时，给他绑定一个矩形 ltrb规定这个矩形
        drawable_merchant.setBounds(0, 0, 75, 75);
        //设置图片在文字的哪个方向
        rbtnMerchant.setCompoundDrawables(null, drawable_merchant, null, null);
        rbtnMerchant.setTextColor(Color.parseColor("#8a8a8a"));

        //定义底部标签图片大小和位置
        Drawable drawable_plan = getResources().getDrawable(R.drawable.plan);
        //当这个图片被绘制时，给他绑定一个矩形 ltrb规定这个矩形
        drawable_plan.setBounds(0, 0, 75, 75);
        //设置图片在文字的哪个方向
        rbtnPlan.setCompoundDrawables(null, drawable_plan, null, null);
        rbtnPlan.setTextColor(Color.parseColor("#8a8a8a"));

        //定义底部标签图片大小和位置
        Drawable drawable_group = getResources().getDrawable(R.drawable.group);
        //当这个图片被绘制时，给他绑定一个矩形 ltrb规定这个矩形
        drawable_group.setBounds(0, 0, 75, 75);
        //设置图片在文字的哪个方向
        rbtnGroup.setCompoundDrawables(null, drawable_group, null, null);
        rbtnGroup.setTextColor(Color.parseColor("#8a8a8a"));

        //定义底部标签图片大小和位置
        Drawable drawable_mine = getResources().getDrawable(R.drawable.mine);
        //当这个图片被绘制时，给他绑定一个矩形 ltrb规定这个矩形
        drawable_mine.setBounds(0, 0, 75, 75);
        //设置图片在文字的哪个方向
        rbtnMine.setCompoundDrawables(null, drawable_mine, null, null);
        rbtnMine.setTextColor(Color.parseColor("#8a8a8a"));
    }
}
